<template>
  <div class="login">
    <div class="bg">
      <div class="box" v-if="!isEnroll">
        <h2>医疗服务系统登录</h2>
        <a-form
          :model="form"
          :style="{ width: '600px' }"
          @submit="handleSubmit"
        >
          <a-form-item field="phone" tooltip="请输入手机号" label="账号">
            <a-input v-model="form.phone" placeholder="请输入手机号" />
          </a-form-item>
          <a-form-item field="passWord" label="密码">
            <a-input-password
              v-model="form.passWord"
              placeholder="请输入密码"
            />
          </a-form-item>
          <a-form-item
            field="roles"
            label="角色"
            :rules="[{ required: true, message: '请选择角色' }]"
          >
            <!-- <a-checkbox v-model="form.isRead">
              I have read the manual
            </a-checkbox> -->
            <a-radio-group v-model="form.role">
              <a-radio value="admin">管理员</a-radio>
              <a-radio value="doctor">医生</a-radio>
              <a-radio value="nurse">护士</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item>
            <div class="btn">
              <a-button html-type="submit" type="primary" long>登录</a-button>
              <a-button
                :style="{ marginLeft: '10px' }"
                type="text"
                @click="handleEnroll"
                >注册</a-button
              >
            </div>
          </a-form-item>
          <div class="tips">
            <p>忘记密码</p>
          </div>
        </a-form>
      </div>
      <div class="box" v-else>
        <h2>注册</h2>
        <a-form
          :model="form"
          :style="{ width: '600px' }"
          @submit="handleSubmit"
        >
          <a-form-item field="userName" tooltip="请输入姓名" label="姓名">
            <a-input v-model="form.userName" placeholder="请输入姓名" />
          </a-form-item>
          <a-form-item field="phone" tooltip="请输入手机号" label="账号">
            <a-input v-model="form.phone" placeholder="请输入手机号" />
          </a-form-item>
          <a-form-item field="passWord" label="密码">
            <a-input-password
              v-model="form.passWord"
              placeholder="请输入密码"
            />
          </a-form-item>
          <a-form-item
            field="roles"
            label="角色"
            :rules="[{ required: true, message: '请选择角色' }]"
          >
            <!-- <a-checkbox v-model="form.isRead">
              I have read the manual
            </a-checkbox> -->
            <a-radio-group v-model="form.role">
              <!-- <a-radio value="admin">管理员</a-radio> -->
              <a-radio value="doctor">医生</a-radio>
              <a-radio value="nurse">护士</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item>
            <div class="btn">
              <a-button html-type="submit" type="primary" long>注册</a-button>
              <a-button
                :style="{ marginLeft: '10px' }"
                type="text"
                @click="handleEnroll"
                >返回</a-button
              >
            </div>
          </a-form-item>
          <div class="tips">
            <p>忘记密码</p>
          </div>
        </a-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { Message } from "@arco-design/web-vue";
import { useLoginStore } from "@/store/login";
import { userApi } from "@/api/index";
import { useRoute, useRouter, RouteRecordRaw } from "vue-router";

const store = useLoginStore();

const router = useRouter();
// console.log(store);
const isEnroll = ref(false);
const form = reactive({
  userName: "",
  phone: "",
  role: "",
  passWord: "",
});
const handleSubmit = (data) => {
  if (isEnroll.value) {
    console.log(data);
    userApi
      .register(data.values)
      .then((res) => {
        console.log(res);

        if (res.code === 200) {
          Message.success({
            content: "注册成功",
            position: "top",
          });
          isEnroll.value = !isEnroll.value;
        } else {
          console.log(res.msg);
        }
      })
      .catch((err) => {
        console.log(err);
        Message.error({
          content: err.msg || "注册失败",
          position: "top",
        });
      });
  } else {
    userApi
      .login(data.values)
      .then((res) => {
        // console.log(res);

        if (res.code === 200) {
          Message.success({
            content: "登录成功",
            position: "top",
          });
          localStorage.setItem("userRole", res.data?.role);
          localStorage.setItem("userJobnumber", res.data?.jobnumber);
          router.push({ name: "Workplace" });
        } else {
          console.log(res.msg);
        }
      })
      .catch((err) => {
        console.log(err);
      });
  }
};
const handleEnroll = () => {
  isEnroll.value = !isEnroll.value;
};
</script>

<style lang="less" scoped>
.login {
  width: 100vw;
  height: 100vh;
}
.bg {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.box {
  position: relative;
  min-width: 600px;
  height: 400px;
  border: 1px solid #eee;
  border-radius: 10px;
  padding: 40px 20px 0 0;
  h2 {
    text-align: center;
    margin-bottom: 50px;
    padding-left: 20px;
  }
  .btn {
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
.tips {
  position: absolute;
  right: 20px;
  bottom: 0px;
  width: 100%;
  display: flex;
  justify-content: end;
  // margin-top: 20px;
  p {
    cursor: pointer;
    color: #666;
  }
}
</style>
